﻿using System;
using System.Windows.Forms;
using DataCenter.Interface.Model;

namespace DataCenter.Component
{
    public partial class DefaultEditForm : Form
    {
        private DefaultEditForm()
        {
            InitializeComponent();
        }
        public DefaultEditForm(ComponentEntity entity, bool isModify)
            :this()
        {
            this.IsModify = isModify;
            this.Entity = entity;
        }
        private bool IsModify;
        private ComponentEntity Entity;

        private void DefaultEditForm_Load(object sender, EventArgs e)
        {
            if (IsModify)
            {
                txtComponentName.Text = Entity.ComponentName;
                txtComponentConfig.Text = Entity.ComponentConfig;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Check()) return;
            Entity.ComponentName = txtComponentName.Text.Trim();
            Entity.ComponentConfig = txtComponentConfig.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool Check()
        {
            bool checkResult = true;
            string errmsg = "";
            TextBox ctrl = null;

            if (string.IsNullOrEmpty(txtComponentName.Text))
            {
                errmsg = "组件名称不能为空";
                ctrl = txtComponentName;
                checkResult = true;
                goto ShowError;
            }

            return true;

        ShowError:
            if (!string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show(errmsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (ctrl != null) ctrl.Focus();
            }
            return checkResult;
        }
    }
}
